package com.ulife.permission.module.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.citrus.service.form.Group;
import com.alibaba.citrus.turbine.Context;
import com.alibaba.citrus.turbine.Navigator;
import com.alibaba.citrus.turbine.dataresolver.FormGroup;
import com.alibaba.citrus.webx.WebxException;
import com.ulife.common.GroupUtils;
import com.ulife.common.Result;
import com.ulife.common.exception.ManagerException;
import com.ulife.permission.dao.dataobject.DepartmentDO;
import com.ulife.permission.manager.DepartmentManager;

public class DepartmentAction {

	private static final Logger log = LoggerFactory.getLogger(DepartmentAction.class);
	
	@Autowired
	private HttpServletRequest request;
	@Autowired
	private DepartmentManager departmentManager;
	
	public void doAddDepartment(@FormGroup("adddepartment") Group group, Context context, Navigator nav) throws WebxException {
		GroupUtils groupUtil = new GroupUtils(group);
		try {
			DepartmentDO department = new DepartmentDO();
			setDepartment(department, groupUtil);
			department.setStatus(0);
			Result<DepartmentDO> result = departmentManager.addDepartment(department);
			if (result == null || !result.isSuccess()) {
				context.put("error", "系统错误");
				return;
			}
		} catch (ManagerException e) {
			context.put("error", "系统错误");
			log.error("doAddDepartment error", e);
		}
		
	}
	
	public void doDelDepartment(@FormGroup("deldepartment") Group group, Context context, Navigator nav) throws WebxException {
		GroupUtils groupUtil = new GroupUtils(group);
		long departmentId = groupUtil.getLong("departmentId");
		int status = groupUtil.getInt("status");
		try {
			if (status < 0 || status > 1 || departmentId < 0) {
				context.put("error", "系统错误");
				return;
			}
			Result<DepartmentDO> depResult = departmentManager.getDepartmentById(departmentId);
			if (depResult != null && depResult.isSuccess() && depResult.getModule() != null) {
				DepartmentDO department = depResult.getModule();
				if (department.getStatus() != status) {
					department.setStatus(status);
					Result<Integer> result = departmentManager.updateDepartment(department);
					if (result == null || !result.isSuccess()) {
						context.put("error", "系统错误");
						return;
					}
				}
			}
			else {
				context.put("error", "系统错误");
				return;
			}
		}
		catch (Exception e) {
			context.put("error", "系统错误");
			log.error("del department error, department id = " + departmentId, e);
		}
	}
	
	public void doUpdateDepartment(@FormGroup("updatedepartment") Group group, Context context, Navigator nav) throws WebxException {
		GroupUtils groupUtil = new GroupUtils(group);
		long departmentId = groupUtil.getLong("departmentId");
		
		try {
			if (departmentId >= 0) {
				Result<DepartmentDO> depResult = departmentManager.getDepartmentById(departmentId);
				if (depResult != null && depResult.isSuccess() && depResult.getModule() != null) {
					DepartmentDO department = depResult.getModule();
					setDepartment(department, groupUtil);
					Result<Integer> updateResult = departmentManager.updateDepartment(department);
					if (updateResult == null || !updateResult.isSuccess()) {
						log.error("update department error, department id = " + departmentId);
						context.put("error", "更新错误");
						return;
					}
				}
			}
			else {
				context.put("error", "系统错误");
				log.error("update department error, department id = " + departmentId);
			}
		} catch (ManagerException e) {
			context.put("error", "系统错误");
			log.error("update department error, department id = " + departmentId, e);
		}
	}
	
	private void setDepartment(DepartmentDO department, GroupUtils group) throws ManagerException {
//		String superiorDepartmentIdStr = group.getString("superiorDepartmentName");
		String departmentName = group.getString("departmentName");
		String note = group.getString("note");
		
//		DepartmentDO superiorDepartment = null;
//		if (StringUtils.isNotBlank(superiorDepartmentIdStr) && StringUtils.isNumeric(superiorDepartmentIdStr)) {
//			long departmentId = Long.parseLong(superiorDepartmentIdStr);
//			Result<DepartmentDO> dResult = departmentManager.getDepartmentById(departmentId);
//			if (dResult != null && dResult.isSuccess() && dResult.getModule() != null) {
//				superiorDepartment = dResult.getModule();
//			}
//			else {
//			}
//		}
//		else {
//		}
		
		if (StringUtils.isNotBlank(departmentName)) {
			department.setDepartmentName(departmentName);
		}
//		if (superiorDepartment != null) {
//			department.setSuperiorDepartmentName(superiorDepartment.getDepartmentName());
//			department.setSuperiorDepartmentId(superiorDepartment.getDepartmentId());
//		}
		if (StringUtils.isNotBlank(note)) {
			department.setNote(note);
		}
	}
	
	/**
	 * 配置部门
	 * @param context
	 * @param nav
	 * @throws WebxException
	 */
	public void doEditDepartment(Context context, Navigator nav) throws WebxException {
		String departmentIdStr = request.getParameter("departmentId");
		String[] roles = request.getParameterValues("role");
		if (StringUtils.isNotBlank(departmentIdStr)) {
			try {
				long departmentId = Long.parseLong(departmentIdStr);
				Result<DepartmentDO> result = departmentManager.getDepartmentById(departmentId);
				if (result != null && result.isSuccess() && null != result.getModule()) {
					DepartmentDO department = result.getModule();
					List<String> rIdList = new ArrayList<String>();
					List<String> rNameList = new ArrayList<String>();
					for (String role : roles) {
						String[] rs = StringUtils.split(role, ":");
						rIdList.add(rs[0]);
						rNameList.add(rs[1]);
					}
					String roleList = StringUtils.join(rIdList, ",");
					String roleNameList = StringUtils.join(rNameList, ",");
					department.setRoleList(roleList);
					department.setRoleNameList(roleNameList);
					Result<Integer> roleResult = departmentManager.updateDepartment(department);
					if (roleResult == null || !roleResult.isSuccess()) {
						context.put("error", "更新角色信息错误");
					}
					else {
						context.put("msg", "保存成功");
					}
				}
			} 
			catch (ManagerException e) {
				context.put("msg", "系统错误");
				log.error("Authorization role error", e);
			}
		}
		else {
			context.put("msg", "更新角色信息错误");
		}
	}
}
